package com.itbour.web.action.stats;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;




















import com.itbour.com.page.Page;
/**
 * 
 */
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.IdNameDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.stats.CouponStatsDto;
import com.itbour.dto.tg.OrgInfoDto;
import com.itbour.dto.trade.InvoiceInfoDto;
import com.itbour.service.stats.CouponStatsService;
import com.opensymphony.xwork2.ActionContext;

public class GetCouponStatsAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private String findYhOrg;						//检索条件[所有组织]
	private String findYhOwner;						//检索条件[所有者]
	HashMap<String, Object> mapCouponResult = new HashMap<String, Object>();// 检索查询出来的结果
	private List<OrgInfoDto> orgComboList;	        // 优惠券持有组织者
	private List<IdNameDto> ownerComboList;	        // 优惠券拥有人
	private CouponStatsService couponStatsService;
	private String findDateStart;// 检索条件[时间范围(起始)]
	private String findDateEnd;// 检索条件[时间范围(截止)]
	private String findYhLot;
	private String findCouponName;
	private List<IdNameDto> lotNoComboList;	// 批次号
	private String orgId;//组织
	private String owner;//人员
	HashMap<String, Object> mapCouponUserCntResult = new HashMap<String, Object>();// 检索查询出来的结果
	HashMap<String, Object> mapCouponOrderCntResult = new HashMap<String, Object>();// 检索查询出来的结果
	HashMap<String, Object> mapCouponPayResult = new HashMap<String, Object>();// 检索查询出来的结果

	public String getCouponStatsInfoPg() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			
			if (StringUtils.isNotBlank(findYhOrg) && Long.valueOf(findYhOrg)>0L) {//并且这个大于零的情况下 
				
				param.put("org_id", findYhOrg);//顶级组织的查询条件 
			}
			if (StringUtils.isNotBlank(findYhOwner)) {//筛选 id --》找到持有人
				param.put("id", findYhOwner);
			}
			if (StringUtils.isNotBlank(findDateStart)) {
				param.put("create_time", findDateStart);
			}
			if (StringUtils.isNotBlank(findDateEnd)) {
				param.put("expiry_time", findDateEnd);
			}
			if (StringUtils.isNotBlank(findYhLot)&&Long.valueOf(findYhLot)>0L) {//筛选 批次号
				param.put("lot_no", findYhLot);
			}
			if (StringUtils.isNotBlank(findCouponName)) {//筛选 描述信息
				param.put("desc", findCouponName);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			//翻页查询
			Page page = couponStatsService.pageQueryStats(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			//writeJSON(resMap);
			mapCouponResult = resMap;
		} else {
			mapCouponResult.clear();
		}
		return SUCCESS;	
	}
	// 获取批次号
	public String getLotNoForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String flg = request.getParameter("blkFlg");
		Map<String, Object> param = new HashMap<String, Object>();
		lotNoComboList = couponStatsService.selectLotNoInfo(param);
		if(lotNoComboList != null && lotNoComboList.size()>0){
			// 添加空白项目
			if("1".equals(flg)){
				IdNameDto blankItem = new IdNameDto();
				blankItem.setId("0");
				blankItem.setName("[不限]");
				lotNoComboList.add(0, blankItem);
			}	
			// 默认选中最新的批次
			lotNoComboList.get(0).setSelected(true);
		}
		return SUCCESS;
	}
	public void downCouponInfo() throws Exception{
		Map<String, Object> param = new HashMap<String, Object>();
		//参数需要时可根据条件筛选导出结果
		if (StringUtils.isNotBlank(findYhOrg) && Long.valueOf(findYhOrg)>0L) {//并且这个大于零的情况下 
			
			param.put("org_id", findYhOrg);//顶级组织的查询条件 
		}

		if (StringUtils.isNotBlank(findYhOwner)) {//筛选 id --》找到持有人
			param.put("id", findYhOwner);
		}
		
		if (StringUtils.isNotBlank(findYhLot)&&Long.valueOf(findYhLot)>0L) {//筛选 批次号
			param.put("lot_no", findYhLot);
		}
		if (StringUtils.isNotBlank(findCouponName)) {//筛选 描述信息
			param.put("desc", findCouponName);
		}
		
		StringBuilder sb = new StringBuilder();
		String SEP = ",";	
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String dataStr = format.format(date);
		String dataStr2 = format2.format(date);
		String fileName = "ITB_COUPON_STATS_"+dataStr2+".csv";

		sb.append("示界港发票详情:").append(SEP).append("\'").append(dataStr).append("\'").append("\n");
		sb.append("持有组织 ")
		.append(SEP).append("持有人 ")
		.append(SEP).append("订单号")
		.append(SEP).append("备注 ")
		.append(SEP).append("持有券数 ")
		.append(SEP).append("兑换用户数 ")
		.append(SEP).append("下单用户数 ")
		.append(SEP).append("订单数 ")
		.append(SEP).append("订单总金额 ")
		.append("\n");
							
		List<CouponStatsDto> dInfoLst = couponStatsService.selectCouponInfo(param);
		
			if(dInfoLst!=null && dInfoLst.size()>0){
				for(int i=0; i<dInfoLst.size(); i++){
					CouponStatsDto	dtlInfo = dInfoLst.get(i);
				sb.append( "\""+Null2Str(dtlInfo.getOrg_name())+"\"" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getNickname())+"\"" )
				.append(SEP).append( "\'"+Null2Str(dtlInfo.getLot_no())+"\'" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getDesc_port())+"\"" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getCouponCnt())+"\"" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getUsercnt())+"\"" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getOrdercnt())+"\"" )
				.append(SEP).append( "\""+Null2Str(dtlInfo.getGoodscnt())+"\"" )
				.append(SEP).append( "\""+Null111Str(dtlInfo.getSummoney())+"\"" )
				.append("\n");
				}
			}
		else {
			sb.append("未获取到优惠券信息！\n");
		}
			
        String output = sb.toString();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.addHeader("Content-Disposition", "attachment; filename="+fileName); 
        response.setContentType("application/octet-stream"); 
        response.setCharacterEncoding("UTF8"); 
        response.getWriter().write(output); //获取
        response.flushBuffer();
        
	}
	private String Null2Str(Object in){
		return (in==null)?"":String.valueOf(in);
	}
	
	private Object Null111Str(Long l){
		return (l==null)?"":Float.valueOf(l)/100;
	}

	// 获取组织信息
	public String getOrgForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String flg = request.getParameter("blkFlg");
		Map<String, Object> param = new HashMap<String, Object>();
		orgComboList = couponStatsService.selectOrgInfo(param);
		if(orgComboList != null && orgComboList.size()>0){
			// 添加空白项目
			if("1".equals(flg)){
				OrgInfoDto blankItem = new OrgInfoDto();
				blankItem.setOrg_id(0L);
				blankItem.setName("[不限]");
				orgComboList.add(0, blankItem);
			}
		}
		return SUCCESS;
	}
	// 获取组织用户
	public String getOrgUserForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String flg = request.getParameter("blkFlg");
		String orgId = request.getParameter("orgId");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("org_id", orgId);
		ownerComboList = couponStatsService.selectOrgUserInfo(param);
		if(ownerComboList != null && ownerComboList.size()>0){
			// 添加空白项目
			if("1".equals(flg)){
				IdNameDto blankItem = new IdNameDto();
				blankItem.setId("0");
				blankItem.setName("[不限]");
				ownerComboList.add(0, blankItem);
			}
			// 默认选中第一个
			ownerComboList.get(0).setSelected(true);
		}
		return SUCCESS;
	}
	public String getUserCntInfoLst() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(orgId)){
				param.put("org_id", orgId);
			}
			if(StringUtils.isNotBlank(owner)){
				param.put("owner", owner);
			}
			if(StringUtils.isNotBlank(findYhLot)&&Long.valueOf(findYhLot)>0L){
				param.put("lot_no", findYhLot);
			}
			if(StringUtils.isNotBlank(findCouponName)){
				param.put("desc", findCouponName);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			//翻页查询
			Page page = couponStatsService.pageQueryUserCnt(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			mapCouponUserCntResult = resMap;
		} else {
			mapCouponUserCntResult.clear();
		}
		return SUCCESS;
		
		
	}
	public String getOrderCntInfoLst() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(orgId)){
				param.put("org_id", orgId);
			}
			if(StringUtils.isNotBlank(owner)){
				param.put("owner", owner);
			}
			if(StringUtils.isNotBlank(findYhLot)&&Long.valueOf(findYhLot)>0L){
				param.put("lot_no", findYhLot);
			}
			if(StringUtils.isNotBlank(findCouponName)){
				param.put("desc", findCouponName);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			//翻页查询
			Page page = couponStatsService.pageQueryOrderCnt(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			mapCouponOrderCntResult = resMap;
		} else {
			mapCouponOrderCntResult.clear();
		}
		return SUCCESS;
		
		
	}
	//下单数量
	public String getPayCntInfoLst() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(orgId)){
				param.put("org_id", orgId);
			}
			if(StringUtils.isNotBlank(owner)){
				param.put("owner", owner);
			}
			if(StringUtils.isNotBlank(findYhLot)&&Long.valueOf(findYhLot)>0L){
				param.put("lot_no", findYhLot);
			}
			
			if(StringUtils.isNotBlank(findCouponName)){
				param.put("desc", findCouponName);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			//翻页查询
			Page page = couponStatsService.pageQueryPayCnt(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			mapCouponPayResult = resMap;
		} else {
			mapCouponPayResult.clear();
		}
		return SUCCESS;
		
		
	}
	
	public String getFindYhOrg() {
		return findYhOrg;
	}

	public String getFindYhOwner() {
		return findYhOwner;
	}

	public HashMap<String, Object> getMapCouponResult() {
		return mapCouponResult;
	}

	public void setFindYhOrg(String findYhOrg) {
		this.findYhOrg = findYhOrg;
	}

	public void setFindYhOwner(String findYhOwner) {
		this.findYhOwner = findYhOwner;
	}

	public void setMapCouponResult(HashMap<String, Object> mapCouponResult) {
		this.mapCouponResult = mapCouponResult;
	}

	public CouponStatsService getCouponStatsService() {
		return couponStatsService;
	}

	public void setCouponStatsService(CouponStatsService couponStatsService) {
		this.couponStatsService = couponStatsService;
	}
	public List<OrgInfoDto> getOrgComboList() {
		return orgComboList;
	}
	public List<IdNameDto> getOwnerComboList() {
		return ownerComboList;
	}
	public void setOrgComboList(List<OrgInfoDto> orgComboList) {
		this.orgComboList = orgComboList;
	}
	public void setOwnerComboList(List<IdNameDto> ownerComboList) {
		this.ownerComboList = ownerComboList;
	}
	public String getFindDateStart() {
		return findDateStart;
	}
	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	public String getFindYhLot() {
		return findYhLot;
	}

	public void setFindYhLot(String findYhLot) {
		this.findYhLot = findYhLot;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getFindCouponName() {
		return findCouponName;
	}
	public void setFindCouponName(String findCouponName) {
		this.findCouponName = findCouponName;
	}
	public List<IdNameDto> getLotNoComboList() {
		return lotNoComboList;
	}
	public void setLotNoComboList(List<IdNameDto> lotNoComboList) {
		this.lotNoComboList = lotNoComboList;
	}
	public String getOrgId() {
		return orgId;
	}
	public String getOwner() {
		return owner;
	}
	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public HashMap<String, Object> getMapCouponUserCntResult() {
		return mapCouponUserCntResult;
	}
	public void setMapCouponUserCntResult(
			HashMap<String, Object> mapCouponUserCntResult) {
		this.mapCouponUserCntResult = mapCouponUserCntResult;
	}
	public HashMap<String, Object> getMapCouponOrderCntResult() {
		return mapCouponOrderCntResult;
	}
	public void setMapCouponOrderCntResult(
			HashMap<String, Object> mapCouponOrderCntResult) {
		this.mapCouponOrderCntResult = mapCouponOrderCntResult;
	}
	public HashMap<String, Object> getMapCouponPayResult() {
		return mapCouponPayResult;
	}
	public void setMapCouponPayResult(HashMap<String, Object> mapCouponPayResult) {
		this.mapCouponPayResult = mapCouponPayResult;
	}

}
